package com.lmonkey.servlet.cart;

import com.lmonkey.servic.*;
import com.lmonkey.shop.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/shangc/okselect")
public class OkSelect extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //菜单
        ArrayList<CATEGORY> flist = CATAGORYDao.selectcat("father");
        request.setAttribute("flist",flist);
        ArrayList<CATEGORY> clist = CATAGORYDao.selectcat("child");
        request.setAttribute("clist",clist);

        HttpSession session = request.getSession();
        ArrayList<Integer> ids1 = (ArrayList<Integer>)session.getAttribute("ids");


        //从数据库中获取数据
        if (ids1 != null){
            ArrayList<PRODUCT> lastlist = PRODUCTDao.selectByid1(ids1);
            request.setAttribute("lastlist",lastlist);
        }

        //获取数据
        String eids = request.getParameter("eids");
        String dzid = request.getParameter("dzid");
        //获取session对象
        String isLogin = (String)session.getAttribute("isLogin");
        USER user = (USER)session.getAttribute("name");

        //判断用户是否登录
        if (user != null && isLogin.equals("1")){
            String uid = user.getuser_id();
            int count = CARTDao.selectByid(uid);
            request.setAttribute("num",count);

            String ids[] = eids.split(",");
            ArrayList<CART> list = new ArrayList<CART>();

            //查找地址
            ADDRESS ads = ADDRESSDao.selectByid3(dzid);

            for (int i = 0;i<ids.length;i++){
                CART es = CARTDao.getcartshop1(ids[i]);
                //修改订单的状态
                ORDERDao.update1(ads.getUser_id(),ads.getUser_name(),es.getCart_id());
            }

            request.getRequestDispatcher("ok.jsp").forward(request,response);
        }else {
            PrintWriter out = response.getWriter();
            out.println("<script>alert('请先登录');location.href='login.jsp';</script>");
            out.close();
            return;
        }
    }
}
